package com.myzl.coal.utils;

import com.google.common.primitives.Bytes;

import java.util.Arrays;

/**
 * @ClassName HexUtil
 * @Author wzb
 * @Date 2022/7/11 14:58
 * @Description
 */
public class HexUtil {

    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                    + Character.digit(s.charAt(i + 1), 16));
        }
        return data;
    }

    public static String bytesToHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte aByte : bytes) {
            String hex = Integer.toHexString(0xFF & aByte);
            if (hex.length() == 1) {
                sb.append('0');
            }
            sb.append(hex);
        }
        String result = sb.toString().toUpperCase();
        return result;
    }

    public static int toInt(byte[] bytes) {
        int ret = 0;
        for (int i = 0; i < 4 && i < bytes.length; i++) {
            ret <<= 8;
            ret |= (int) bytes[i] & 0xFF;
        }
        return ret;
    }

    public static byte[] reverse(byte[] bytes) {
        Bytes.reverse(bytes);
        return bytes;
    }

    public static int bytes2Int(byte[] b, int start,int len) {
        int sum = 0;
        int end = start + len;
        for (int i = start; i < end; i++) {
            int n = ((int)b[i]) & 0xff;
            n <<= (--len) * 8;
            sum += n;
        }
        return sum;
    }

    public static byte[] getBytes(byte[] bytes, int i) {
        return getBytes(bytes, i, 1);
    }

    public static String getString(byte[] bytes, int i, int j) {
        byte[] bytes1 = getBytes(bytes, i, j);
        return bytesToHexString(bytes1);
    }

    public static Integer getInt(byte[] bytes, int i, int j) {
        byte[] bytes1 = getBytes(bytes, i, j);
        return toInt(bytes1);
    }

    public static Long toLong(byte[] bytes, int offset) {
        long result = 0;
        for (int i=0; i<8 && i+offset<bytes.length; i++) {
            result <<= 8;
            result |= (bytes[i] & 0xFF);
        }
        return result;
    }

    public static Long toLong(byte[] bytes) {
        return toLong(bytes,0);
    }

    public static Long getLong(byte[] bytes, int i, int j) {
        byte[] bytes1 = getBytes(bytes, i, j);
        return toLong(bytes1);
    }

    public static byte[] getBytes(byte[] bytes, int i, int j) {
        return Arrays.copyOfRange(bytes, i, i + j);
    }


    public static int toInt(byte dataByte) {
        return (int) dataByte & 0xFF;
    }

    public static byte[] concat(byte[]... arrays) {
        int length = 0;
        byte[][] var2 = arrays;
        int pos = arrays.length;

        for(int var4 = 0; var4 < pos; ++var4) {
            byte[] array = var2[var4];
            length += array.length;
        }

        byte[] result = new byte[length];
        pos = 0;
        byte[][] var9 = arrays;
        int var10 = arrays.length;

        for(int var6 = 0; var6 < var10; ++var6) {
            byte[] array = var9[var6];
            System.arraycopy(array, 0, result, pos, array.length);
            pos += array.length;
        }

        return result;
    }
}
